package com.wangzhixuan.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wangzhixuan.commons.base.BaseController;
import com.wangzhixuan.commons.utils.PageInfo;
import com.wangzhixuan.commons.utils.UUIDGenerator;
import com.wangzhixuan.model.Dictionary;
import com.wangzhixuan.service.IDictionaryService;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhixuan.wang
 * @since 2017-04-16
 */
@Controller
@RequestMapping("/dictionary")
public class DictionaryController extends BaseController {

    @Autowired
    private IDictionaryService dictionaryService;

    @RequestMapping("/manager")
    public String manager() {
        return "admin/dictionary/dictionaryList";
    }


    @RequestMapping("/dataGrid")
    @ResponseBody
    public PageInfo dataGrid(Dictionary dictionary, Integer page, Integer rows, String sort, String order) {
        PageInfo pageInfo = new PageInfo(page, rows, sort, order);
        Map<String, Object> condition = new HashMap<String, Object>();

        condition.put("companyCode", getCompanyCode());
        if(dictionary.getCatalogId()!=null){
        	condition.put("catalogId", dictionary.getCatalogId());
        }
        pageInfo.setCondition(condition);
        dictionaryService.selectDataGrid(pageInfo);
        return pageInfo;
    }

    /**
     * 添加页面
     *
     * @return
     */
    @RequestMapping("/addPage")
    public String addPage() {
        return "admin/dictionary/dictionaryAdd";
    }

    /**
     * 添加
     *
     * @param
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public Object add(Dictionary dictionary) {
        dictionary.setCompanyCode(getCompanyCode());
        dictionary.setDicCode(UUIDGenerator.getUUID());
        dictionary.setState("1");
        dictionary.setShowIndex(0);
        boolean b = dictionaryService.insert(dictionary);
        if (b) {
            return renderSuccess("添加成功！");
        } else {
            return renderError("添加失败！");
        }
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(Long id) {
        boolean b = dictionaryService.deleteById(id);
        if (b) {
            return renderSuccess("删除成功！");
        } else {
            return renderError("删除失败！");
        }
    }

    /**
     * 编辑
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/editPage")
    public String editPage(Model model, Long id) {
        Dictionary dictionary = dictionaryService.selectById(id);
        model.addAttribute("dictionary", dictionary);
        return "admin/dictionary/dictionaryEdit";
    }

    /**
     * 编辑
     *
     * @param
     * @return
     */
    @RequestMapping("/edit")
    @ResponseBody
    public Object edit(Dictionary dictionary) {
        boolean b = dictionaryService.updateById(dictionary);
        if (b) {
            return renderSuccess("编辑成功！");
        } else {
            return renderError("编辑失败！");
        }
    }
    
    /**
     * 查询列表
     *
     * @param createId
     * @return
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String catalogId) {
        Map<String, Object> map = new HashMap<>();
        map.put("catalog_id", catalogId);
        map.put("company_code", getCompanyCode());
        map.put("state", "1");
        List<Dictionary> list = dictionaryService.selectByMap(map);
        return list;
    }
    
    @RequestMapping("/qcode")
    public String qcode(HttpServletRequest request,Model model, Dictionary dictionary) {
    	String baseUrl = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort();
        model.addAttribute("dictionary", dictionary);
        model.addAttribute("baseUrl", baseUrl);
        return "admin/dictionary/qcode";
    }

}
